package SGEngine

/////参考 http://www.angelcode.com/products/bmfont/doc/file_format.html
import (
	"bufio"
	//"fmt"

	"os"
	"strconv"
	"strings"
)

type Font struct {
	BMFontData *BMFont
}

type FontChar struct {
	TexRect  Rect
	SizeRect Rect
	BMChar   *BMGlyph
	POS      Vec2
}

//BMFont///////////////////////////////////////////////////////
type BMFont struct {
	Fonts       map[int]*BMGlyph
	FontTexture *ACTexture
	ImageName   string
	Face        string
	FontNum     int
	Size        int
	ScaleW      int
	ScaleH      int
	SpacingH    int
	SpacingV    int
	LineHeight  int
	Base        int
}

type BMGlyph struct {
	Index   int
	X       int
	Y       int
	Width   int
	Height  int
	OffsetX int
	OffserY int
	Advance int
	Channel int
}

var (
	DefaultFont *Font
)

func init() {

}

func LoadFont(BMFileName string) {
	DefaultBMFont := ReadTextToBMFont(BMFileName)

	DefaultFont = new(Font)
	DefaultFont.BMFontData = DefaultBMFont

	patharr := strings.Split(BMFileName, "/")
	Str := strings.Replace(BMFileName, patharr[len(patharr)-1], "", 1)

	DefaultFont.BMFontData.FontTexture = ACTextureMgr.LoadTexture(Str + DefaultBMFont.ImageName)
}

func ReadTextToBMFont(TextPath string) *BMFont {
	RFont := new(BMFont)
	RFont.Fonts = make(map[int]*BMGlyph)
	f, Err := os.Open(TextPath)
	CheckErr(Err)
	rf := bufio.NewReader(f)

	_ReadHead(rf, RFont)

	for {
		linstr, err := rf.ReadString('\n')
		if err != nil {
			break
		}

		_ReadLineToBMGlyph(linstr, RFont)

	}

	return RFont
}

func _ReadHead(rf *bufio.Reader, _bmfont *BMFont) {
	//Line1
	Line1, _ := rf.ReadString('\n')
	Line1Arr := strings.Split(Line1, "=")
	_bmfont.Face = strings.Trim(strings.Split(Line1Arr[1], " ")[0], " ")
	_bmfont.Size, _ = strconv.Atoi(strings.Trim(strings.Split(Line1Arr[2], " ")[0], " "))
	strSpac := strings.Trim(strings.Split(Line1Arr[11], " ")[0], " ")
	_bmfont.SpacingH, _ = strconv.Atoi(strings.Split(strSpac, ",")[0])
	_bmfont.SpacingV, _ = strconv.Atoi(strings.Split(strSpac, ",")[1])
	//Line2
	Line2, _ := rf.ReadString('\n')
	Line2Arr := strings.Split(Line2, "=")
	_bmfont.ScaleW, _ = strconv.Atoi(strings.Trim(strings.Split(Line2Arr[3], " ")[0], " "))
	_bmfont.ScaleH, _ = strconv.Atoi(strings.Trim(strings.Split(Line2Arr[4], " ")[0], " "))
	_bmfont.LineHeight, _ = strconv.Atoi(strings.Trim(strings.Split(Line2Arr[1], " ")[0], " "))
	_bmfont.Base, _ = strconv.Atoi(strings.Trim(strings.Split(Line2Arr[2], " ")[0], " "))
	//Line3
	Line3, _ := rf.ReadString('\n')
	imgname := strings.Trim(strings.Split(Line3, "=")[2], `"`)
	_bmfont.ImageName = imgname[0 : len(imgname)-3]
	Line3 = Line3
	//Line4 字符数.
	Numstr, _ := rf.ReadString('\n')
	NumArr := strings.Split(Numstr, "=")
	_bmfont.FontNum, _ = strconv.Atoi(strings.Trim(NumArr[1], "\r\n"))

}

func _ReadLineToBMGlyph(Linstr string, _bmfont *BMFont) {
	LinstrArr := strings.Split(Linstr, "=")

	bmglyph := new(BMGlyph)
	bmglyph.Index, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[1], " ")[0], " "))
	bmglyph.X, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[2], " ")[0], " "))
	bmglyph.Y, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[3], " ")[0], " "))
	bmglyph.Width, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[4], " ")[0], " "))
	bmglyph.Height, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[5], " ")[0], " "))
	bmglyph.OffsetX, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[6], " ")[0], " "))
	bmglyph.OffserY, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[7], " ")[0], " "))
	bmglyph.Advance, _ = strconv.Atoi(strings.Trim(strings.Split(LinstrArr[8], " ")[0], " "))
	bmglyph.Channel, _ = strconv.Atoi(strings.Trim(LinstrArr[10], "\r\n"))
	_bmfont.Fonts[bmglyph.Index] = bmglyph

}

func (b *BMFont) GetText(_text string) *BMGlyph {
	r := []rune(_text)

	return b.Fonts[int(r[0])]
}

func (b *BMFont) GetTextByRune(_text rune) *BMGlyph {
	return b.Fonts[int(_text)]
}

func (b *BMFont) GetGlyphArr(_text string) []*BMGlyph {

	return nil
}

//////////////////////////////////////////////////////////////////////////////////////////////////
func (b *BMFont) BMCharsToFile(fname string) {
	f, err := os.Create(fname)
	CheckErr(err)
	wf := bufio.NewWriter(f)
	for k, _ := range b.Fonts {
		wf.WriteString(string(k))
	}
}
